﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qianwei.Model;
using Qianwei.Dac;
using Qianwei.Common;
using Qianwei.Common.Extension;
using MySql.Data.MySqlClient;

namespace Qianwei.Biz
{
    public class NewsCenterManager : ManagerBase<NewsCenterManager>
    {
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<news_center> QueryNewsCenter(string strWhere)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM news_center ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            IList<news_center> nclist = Mapper.DataReaderToList<news_center>(msh.ExecuteReader(strSql.ToString()));
            return nclist;
        }

        public IList<news_center> QueryNewsCenter(string strWhere, string filedOrder)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM news_center ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            IList<news_center> nclist = Mapper.DataReaderToList<news_center>(msh.ExecuteReader(strSql.ToString()));
            return nclist;
        }

        /// <summary>
        /// 获取前几行数据
        /// </summary>
        /// <param name="Top"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
        public IList<news_center> QueryNewsCenter(int Top, string strWhere, string filedOrder)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            strSql.Append(" * ");
            strSql.Append(" FROM news_center ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            if (Top > 0)
            {
                strSql.Append(" limit " + Top.ToString());
            }
            IList<news_center> nclist = Mapper.DataReaderToList<news_center>(msh.ExecuteReader(strSql.ToString()));
            return nclist;
        }

        public int Add(news_center model)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into news_center(");
            strSql.Append("New_Type,New_Title,New_Author,New_Content,Create_Time,Last_Change_Time");
            strSql.Append(") values (");
            strSql.Append("@New_Type,@New_Title,@New_Author,@New_Content,@Create_Time,@Last_Change_Time");
            strSql.Append(") ");
            strSql.Append(";SELECT LAST_INSERT_ID();");
            MySqlParameter[] parameters = {
			            new MySqlParameter("@New_Type", (object)model.New_Type) ,            
                        new MySqlParameter("@New_Title", model.New_Title) ,            
                        new MySqlParameter("@New_Author", model.New_Author) ,            
                        new MySqlParameter("@New_Content", model.New_Content) ,            
                        new MySqlParameter("@Create_Time", model.Create_Time) ,            
                        new MySqlParameter("@Last_Change_Time", model.Last_Change_Time)             
            };

            object obj = msh.GetSingle(strSql.ToString(), parameters);
            return obj.TryInt();
        }

        public bool Update(news_center model)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update news_center set ");

            strSql.Append(" New_Type = @New_Type , ");
            strSql.Append(" New_Title = @New_Title , ");
            strSql.Append(" New_Author = @New_Author , ");
            strSql.Append(" New_Content = @New_Content , ");
            strSql.Append(" Last_Change_Time = @Last_Change_Time  ");
            strSql.Append(" where New_Center_Id=@New_Center_Id ");

            MySqlParameter[] parameters = {
			            new MySqlParameter("@New_Center_Id", (object)model.New_Center_Id) ,            
                        new MySqlParameter("@New_Type", (object)model.New_Type) ,            
                        new MySqlParameter("@New_Title", model.New_Title) ,            
                        new MySqlParameter("@New_Author", model.New_Author) ,            
                        new MySqlParameter("@New_Content", model.New_Content) ,            
                        new MySqlParameter("@Last_Change_Time", model.Last_Change_Time) 
              
            };
            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Delete(int New_Center_Id)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from news_center ");
            strSql.Append(" where New_Center_Id=@New_Center_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@New_Center_Id", (object)New_Center_Id)
            };

            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteList(string New_Center_Idlist)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from news_center ");
            strSql.Append(" where ID in (" + New_Center_Idlist + ")  ");
            int rows = msh.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public news_center GetModel(int New_Center_Id)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select New_Center_Id, New_Type, New_Title, New_Author, New_Content, Create_Time, Last_Change_Time  ");
            strSql.Append("  from news_center ");
            strSql.Append(" where New_Center_Id=@New_Center_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@New_Center_Id", (object)New_Center_Id)
            };

            news_center newsinfo = Mapper.DataReaderToObject<news_center>(msh.ExecuteReader(strSql.ToString(), parameters));
            return newsinfo;
        }
    }
}
